package thread.concurrents.aqs;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class ConditionDemo {

  public static void main(String[] args) {
    ReentrantLock lock = new ReentrantLock();

    Condition condition = lock.newCondition();

    int a = 0;

    new Thread(new Runnable() {

      @Override
      public void run() {
        try {
          lock.lock();
          condition.await();
          System.out.println(Thread.currentThread().getName() + " " + "await 1 ");
        } catch (InterruptedException e) {
          e.printStackTrace();
        } finally {
          lock.unlock();
        }
      }


    }).start();

    new Thread(new Runnable() {

      @Override
      public void run() {
        try {
          lock.lock();
          condition.await();
          System.out.println(Thread.currentThread().getName() + " " + "await 2 ");
        } catch (InterruptedException e) {
          e.printStackTrace();
        } finally {
          lock.unlock();
        }
      }


    }).start();

    new Thread(new Runnable() {

      @Override
      public void run() {
        try {
          lock.lock();
          System.out.println(Thread.currentThread().getName() + " " + "signal 2 ");
//          condition.signal();
          condition.signalAll();
        } catch (Exception e) {
          e.printStackTrace();
        } finally {
          lock.unlock();
        }
      }

    }).start();


  }


}
